package com.kmxd.ams.app.logs;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.logs.ILogSearchSvc;
import com.kmxd.ams.client.logs.dto.LogSearchAddDTO;
import com.kmxd.ams.client.logs.dto.LogSearchEditDTO;
import com.kmxd.ams.client.logs.dto.LogSearchQueryDTO;
import com.kmxd.ams.client.logs.vo.LogSearchListVO;
import com.kmxd.ams.client.logs.vo.LogSearchVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.logs.entity.LogSearch;
import com.kmxd.ams.infra.logs.mapper.LogSearchMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 条目检索日志表 服务实现类
 *
 * @author nickbi
 * @since 2022-12-23
 */
@Service
@AllArgsConstructor
public class LogSearchSvcImpl extends ServiceImpl<LogSearchMapper, LogSearch>
    implements ILogSearchSvc {

  private LogSearchMapper logSearchMapper;

  @Override
  public Page<LogSearchListVO> selectPage(Page reqPage, LogSearchQueryDTO req) {
    LambdaQueryWrapper<LogSearch> queryWrapper =
        QueryGen.init(new QueryWrapper<LogSearch>(), req).lambda();
    queryWrapper.orderByDesc(LogSearch::getCreateTime);
    Page<LogSearch> page = logSearchMapper.selectPage(reqPage, queryWrapper);
    Page<LogSearchListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), LogSearchListVO.class));
    return voPage;
  }

  @Override
  public List<LogSearchListVO> selectList(LogSearchQueryDTO req) {
    LambdaQueryWrapper<LogSearch> queryWrapper =
        QueryGen.init(new QueryWrapper<LogSearch>(), req).lambda();
    queryWrapper.orderByDesc(LogSearch::getCreateTime);
    List<LogSearch> list = logSearchMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, LogSearchListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(LogSearchAddDTO req) {
    LogSearch entity = BeanUtil.copyProperties(req, LogSearch.class);
    logSearchMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(LogSearchEditDTO req) {
    LogSearch entity = BeanUtil.copyProperties(req, LogSearch.class);
    logSearchMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public LogSearchVO view(Long id) {
    LogSearch entity = logSearchMapper.selectById(id);
    return BeanUtil.copyProperties(entity, LogSearchVO.class);
  }
}
